package com.zhkj.txg.utils

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import androidx.core.content.ContextCompat
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.MonthView
import com.zhkj.lib.utils.ScreenUtil
import com.zhkj.txg.R
import kotlin.math.min

class CustomMonthView(context: Context) : MonthView(context) {

    private val curDayBackgroundPaint = Paint().apply {
        isAntiAlias = true
        style = Paint.Style.FILL
        color = ContextCompat.getColor(context, R.color.colorGradientMain)
    }

    private val mSchemeBasicPaint = Paint().apply {
        isAntiAlias = true
        style = Paint.Style.FILL
        color = ContextCompat.getColor(context, R.color.colorMain)
    }
    private val mPadding: Int = ScreenUtil.dip(context, 4)
    private val radius: Int = ScreenUtil.dip(context, 3)

    override fun onDrawText(
        canvas: Canvas?, calendar: Calendar?, x: Int, y: Int,
        hasScheme: Boolean, isSelected: Boolean
    ) {
        val cx = x + mItemWidth / 2
        // 先绘制圆圈
        if (calendar?.isCurrentDay == true) {
            canvas?.drawCircle(
                cx.toFloat(), (y + min(mItemHeight, mItemWidth) / 2).toFloat(),
                (min(mItemHeight, mItemWidth) / 2 - mPadding).toFloat(), curDayBackgroundPaint
            )
        }
        // 再绘制文字
        canvas?.drawText(
            calendar?.day.toString(), cx.toFloat(), mTextBaseLine + y,
            when {
                calendar?.isCurrentDay == true -> mCurDayTextPaint
                calendar?.hasScheme() == true -> mSchemeTextPaint
                else -> mCurMonthTextPaint
            }
        )
    }

    override fun onDrawSelected(canvas: Canvas?, calendar: Calendar?, x: Int, y: Int, hasScheme: Boolean): Boolean =
        true

    override fun onDrawScheme(canvas: Canvas?, calendar: Calendar?, x: Int, y: Int) {
        canvas!!.drawCircle(
            (x + mItemWidth / 2).toFloat(),
            (mTextBaseLine + y + mPadding * 2),
            radius.toFloat(),
            mSchemeBasicPaint
        )
    }
}